<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Dashboard extends Backend {

	public function __construct()
	{
		parent::__construct();
		$this->load->model(['siteorder_model']);
		$this->load->model(['site_model']);
		$this->load->model(['hotproduct_model']);
		//
		$this->load->helper('number');
		$this->load->model('resources_model');

		//
		$this->data['page_home'] = lang('lang_dashboard');
	}

	public function nillproduct()
	{

		// Title
		$this->data['meta_title'] = $this->data['page_home'];

		// Breadcrumbs
		$this->data['array_breadcrumbs'] = array(
			$this->data['page_home'] => 'backend'
		);


		$page_data=$this->hotproduct_model->getnill(30);
		$this->data['data']=$page_data;

		$this->data['sites']=$this->site_model->getall();
		$this->data['sindex']=$this->site_model->get_name_index();


		$this->data['content_header_title'] = content_header_title($this->data['page_home']);
		$this->data['content'] = 'nillproduct';

		$this->render();
	}


	public function hotproduct()
	{
		// Title
		$this->data['meta_title'] = $this->data['page_home'];

		// Breadcrumbs
		$this->data['array_breadcrumbs'] = array(
			$this->data['page_home'] => 'backend'
		);

		$timelimit=$this->input->get("timelimit");

		if(!$timelimit)
		{
			$timelimit=[
				"start"=>date("Y-m-d",strtotime("-6 days")),
				"end"=>date("Y-m-d")
			];
		}
		$key=trim($this->input->get('keyword'));
		$siteid=trim($this->input->get('siteid'));
		
		$page=max(1,intval($this->input->get("page")));
		
		$page_data=$this->hotproduct_model->getpage($timelimit,$page,$key,$siteid);

		$this->data['data']=$page_data["data"];
		$this->data['pager']=$page_data["pager"];

		$this->data['sites']=$this->site_model->getall();
		$this->data['sindex']=$this->site_model->get_name_index();
		$this->data['surlindex']=$this->site_model->get_url_index();
		
		$this->data["timerange"]=$timelimit;
		//
		$this->data['content_header_title'] = content_header_title($this->data['page_home']);
		$this->data['content'] = 'hotproduct';
		$keyword = array(
			'name'      => 'keyword',
			'id'        => 'keyword',
			'value'     =>  $key,
			'class'     =>  'form-control input-sm',
			'placeholder'=> '请输入关键字',
			'maxlength' => '100'
		);
		$this->data['keyword']=$keyword;

		//
		$this->render();


	}

	public function index()
	{
		/*
		// Get disk informations
		$this->data['disk_total']       = byte_format($this->resources_model->get_disk_total("./"), 3);
		$this->data['disk_use']         = byte_format($this->resources_model->get_disk_use("./"), 3);
		$this->data['disk_free']        = byte_format($this->resources_model->get_disk_free("./"), 3);
		$this->data['disk_use_percent'] = $this->resources_model->get_disk_use_percent("./");

		// Get memory informations
		$this->data['memory_total']       = byte_format($this->resources_model->get_memory_total(), 3);
		$this->data['memory_use']         = byte_format($this->resources_model->get_memory_use(), 3);
		$this->data['memory_free']        = byte_format($this->resources_model->get_memory_free(), 3);
		$this->data['memory_use_percent'] = $this->resources_model->get_memory_use_percent();
		*/

		$days=[];
		for($i=14;$i>=0;$i--)
		{   
			$days[]=date('Y-m-d', strtotime('-'.$i.' day'));
		}
		$this->data['days']=$days;
		$reuslt = $this->siteorder_model->tongji($days);
		$this->data['tongji']=$reuslt["total"];
		$this->data['odata']=$reuslt["data"];
		$this->data['sites']=$this->site_model->getall();
		$msg="";
		if (isset($_POST) && ! empty($_POST))
        {
			$timelimit=$this->input->post("timelimit");
			$siteid=$this->input->post("siteid");
			$sepmsg="";
			if($siteid>0)
			{
				$site=$this->site_model->getitembyid($siteid);
				$sitename=$site["name"];
			}
			else
			{
				$sitename="所有站点";
				$sitetype = $this->config->item('site_types');
				foreach($sitetype as $k=>$type)
				{
					$sid=[];
					$sites=$this->site_model->getsitetype($k);
					foreach($sites as $s)
					{
						$sid[]=$s["id"];
					}
					$result=$this->siteorder_model->getrangesum($timelimit,$sid);
					$sepmsg.="，".$type."站点，询单：".($result["cnt"]?$result["cnt"]:0)." 条";
				}

			}
			$result = $this->siteorder_model->getrangesum($timelimit,$siteid);
			if($result)
			{
			
				
				$msg="{$sitename} , 开始时间：".$timelimit["start"]." , 结束时间：".$timelimit["end"]."，共包含 ".$result["cnt"]." 个询单".($sepmsg?$sepmsg:"。");
				
			}
		}
		$this->data['result']=$msg;
		// Title
		$this->data['meta_title'] = $this->data['page_home'];

		// Breadcrumbs
		$this->data['array_breadcrumbs'] = array(
			$this->data['page_home'] => 'backend'
		);

		//
		$this->data['content_header_title'] = content_header_title($this->data['page_home']);
		$this->data['content'] = 'dashboard';

		//
		$this->render();
	}
}
